#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include<pthread.h>
#include "DecoderProcess.h"

#define msleep(ms) usleep(ms*1000)


/**
 * decoder - thread operation.
 *	@param : thread parameter
 */
void * decoder(void * param)
{
    kapok_hardware_tidecoder::DecoderProcess dec;
    int rdfd;
    char sdata[103000] = {0};
    rdfd = open("video.h264", O_RDONLY);
    //rdfd = open("v960.h264", O_RDONLY);
    struct timeval times;
    long curr_time = 0;         /* NOLINT */
    long prev_time = 0;  /* NOLINT */
    char *yuv_buf = (char *)malloc(640*360*3/2);
    int yuv_buf_size = 640*360*3/2;
    //char *yuv_buf = (char *)malloc(960*540*3/2);
    //int yuv_buf_size = 960*540*3/2;
    printf("yuv buf len:%d\n", yuv_buf_size);

    //read(rdfd, sdata+3768+829, 1346);
    read(rdfd, sdata, 3768);
    dec.DecoderInit(640, 360);
    //read(rdfd, sdata, 102193);
    //dec.DecoderInit(960, 540);

	while (1) {
        printf("************ info ************\n");
        gettimeofday(&times, NULL);        
        prev_time = (times.tv_sec * 1000000 + times.tv_usec);                
        dec.DecoderH264(sdata, 3768, yuv_buf, yuv_buf_size, 123456);    
        gettimeofday(&times, NULL);        
        curr_time = (times.tv_sec * 1000000 + times.tv_usec);
        printf("decoder time is %ld us\n", (curr_time - prev_time));
        msleep(33);
	}
    dec.DecoderUnInit();
}

/*
 * abstruct : main program.
 */

int main(int argc, char ** argv)
{
	int com;
	unsigned char buffer[] = "123";
	pthread_t decoder_thread_id;

	void * ptr;
	pthread_attr_t attr;

	#if 1
	pthread_attr_init(&attr);

	decoder_thread_id = 0;
	pthread_create(&decoder_thread_id, &attr, decoder, NULL);
	msleep(1000);
	#endif	

	pthread_join(decoder_thread_id, & ptr);

	return 0;
}

